package com.derbysoft.lowcostadapter.dswitch3.service.impl;

import com.derbysoft.common.util.CollectionUtils;
import com.derbysoft.dswitch.dto.hotel.common.GuaranteeDTO;
import com.derbysoft.dswitch.dto.hotel.common.TotalDTO;
import com.derbysoft.dswitch.dto.hotel.reservation.ReservationRoomStayDTO;
import com.derbysoft.dswitch.remote.hotel.dto.HotelReservationRequest;
import com.derbysoft.lowcostadapter.commons.dto.BookRequest;
import com.derbysoft.lowcostadapter.commons.dto.Payment;
import com.derbysoft.lowcostadapter.dswitch3.translate.impl.CreditCardType;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * User: jason
 * Date: 2013-04-07
 */
public class PaymentHelper {
    private static final String BLANK = " ";
    private static final double SURCHARGE_PERCENTAGE = 0.01;

    public static Payment translatePayment(BookRequest bookRequest, HotelReservationRequest reservationRequest) {
        ReservationRoomStayDTO roomStayDTO = reservationRequest.getHotelReservationRQ().getHotelReservation().getRoomStaysList().get(0);
        List<GuaranteeDTO> guarantees = roomStayDTO.getGuaranteesList();
        if (CollectionUtils.isEmpty(guarantees)) {
            return null;
        }
        GuaranteeDTO guarantee = guarantees.get(0);
        if (guarantee.getGuaranteeType() != GuaranteeDTO.GuaranteeType.CreditCard) {
            return null;
        }
        if (!CreditCardType.containsType(guarantee.getPaymentCard().getCardCode())) {
            return null;
        }
        TotalDTO total = roomStayDTO.getTotal();
        if (total == null) {
            return null;
        }
        Payment payment = new Payment();
        payment.setCardHoldersName(guarantee.getPaymentCard().getCardHolderName());
        payment.setCardNumber(guarantee.getPaymentCard().getCardNumber());
        payment.setCreditCardTypeID(CreditCardType.getCode(guarantee.getPaymentCard().getCardCode()));
        int month = Integer.parseInt(guarantee.getPaymentCard().getExpireDate().substring(0, 2));
        int year = Integer.parseInt(guarantee.getPaymentCard().getExpireDate().substring(2));
        payment.setExpireMonth(month);
        payment.setExpireYear(year);
        payment.setCurrencyCode(total.getCurrency());
        payment.setAmount(bookRequest.getBookingDetails().getExpectedTotal());
        payment.setSurcharge(calcSurcharge(payment));
        payment.setTotalPayment(payment.getAmount().add(payment.getSurcharge()));
        payment.setSecurityCode(BLANK);
        payment.setAddress1(getAddress(roomStayDTO));
        payment.setPostcode(BLANK);
        payment.setTownCity(BLANK);
        return payment;
    }

    private static String getAddress(ReservationRoomStayDTO roomStayDTO) {
        if (CollectionUtils.isNotEmpty(roomStayDTO.getContactPersonsList())
                && CollectionUtils.isNotEmpty(roomStayDTO.getContactPersonsList().get(0).getAddressesList())) {
            StringBuilder builder = new StringBuilder();
            for (String address : roomStayDTO.getContactPersonsList().get(0).getAddressesList()) {
                builder.append(address);
            }
            return builder.toString();
        }
        return BLANK;
    }

    private static BigDecimal calcSurcharge(Payment payment) {
        return payment.getAmount().multiply(BigDecimal.valueOf(SURCHARGE_PERCENTAGE)).setScale(2, RoundingMode.HALF_UP);
    }
}
